package service;

import common.Pagination;
import common.StringUtils;
import dao.CroChangegoodsDao;
import dao.CroReturngoodsDao;
import entity.CroChangegoods;
import entity.CroReturngoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroChangegoodsVo;
import vo.CroReturngoodsVo;

import java.util.List;

/**
 * Created by wsx on 2017/5/13.
 */
@Service
public class CroChangegoodService {

    @Autowired
    private CroChangegoodsDao croChangegoodsDao;

    /**
     * 前台添加换货申请
     * @param goods
     * @return
     */
    @Transactional(readOnly = false)
    public boolean addReturnGoods(CroChangegoods goods){
        try {
            croChangegoodsDao.save(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /*
    * 分页获取所有的换货货申请
    * */
    @Transactional(readOnly = true)
    public Pagination<CroChangegoods> findAllChangegoods(int index, int size){
        String hql="from CroChangegoods where del_flag='0'";
        return croChangegoodsDao.findPagination(hql,index,size);
    }

    /*
   * 分页查找获取所有的换货申请
   * */
    @Transactional(readOnly = true)
    public Pagination<CroChangegoods> findChangegoodsByConditction(int index, int size, CroChangegoodsVo croChangegoodsVo){
        StringBuffer sb=new StringBuffer();
        sb.append("from CroChangegoods where del_flag='0'");
        if(StringUtils.isNotBlank(croChangegoodsVo.getChangeStatus())&&!(croChangegoodsVo.getChangeStatus().equals("-1"))){
            sb.append(" and change_status='"+croChangegoodsVo.getChangeStatus()+"'");
        }
        if(StringUtils.isNotBlank(croChangegoodsVo.getUsername())){
            sb.append(" and croPurchase.croCustomer.username like'%"+croChangegoodsVo.getUsername()+"%'");
        }
        if(StringUtils.isNotBlank(croChangegoodsVo.getPurchaseid())){
            sb.append(" and croPurchase.id='"+croChangegoodsVo.getPurchaseid()+"'");
        }
        return croChangegoodsDao.findPagination(sb.toString(),index,size);
    }
    /*
      * 删除换货申请
      * */
    @Transactional(readOnly = false)
    public Boolean deleteChangegoods(String id){
        CroChangegoods croChangegoods=croChangegoodsDao.getById(id);
        croChangegoods.setDel_flag("1");
        return true;

    }

    /*
    * 获取换货商品的详情
    * */
    @Transactional(readOnly = true)
    public CroChangegoods getChangegoodsDetail(String id){
        List<CroChangegoods> croReturngoodsList;
        StringBuffer sb=new StringBuffer();
        sb.append("from CroChangegoods where del_flag='0' and id='"+id+"'");
        croReturngoodsList=croChangegoodsDao.findByHqlExt(sb.toString(),CroChangegoods.class);
        return croReturngoodsList.get(0);
    }
    /*
* 改变换货状态
* */
    @Transactional(readOnly = false)
    public Boolean changeGoodsStatus(String id,String status){
        if(StringUtils.isNotBlank(status)) {
            CroChangegoods croChangegoods=croChangegoodsDao.getById(id);
            croChangegoods.setChange_status(status);
            croChangegoodsDao.update(croChangegoods);
        }
        return true;
    }
}
